package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;

@Controller
@RequestMapping("/cargo/packing")
@Slf4j
public class PackingController extends BaseController {

    // 注入dubbo的服务接口的代理对象
    @Reference
    private ContractService contractService;
    @Reference
    private PackingService packingService;

    @Reference
    private ExportService exportService;

    /**
     * 报运单列表
     */
    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize) {
        log.info("执行报运单列表分页的方法");

        // 构造查询对象
        PackingExample example = new PackingExample();
        example.setOrderByClause("create_time desc");
        PackingExample.Criteria criteria = example.createCriteria();
        // 添加查询条件：compnay_id
        criteria.andCompanyIdEqualTo(getLoginCompanyId());

        PageInfo<Packing> pageInfo =
                packingService.findByPage(example, pageNum, pageSize);
        ;
        // 存储查询结果
        request.setAttribute("pageInfo", pageInfo);

        // 进入装箱管理列表
        return "cargo/packing/packing-list";
    }


    /**
     * 功能描述：
     * 1、必须已报运的数据，才可以点击生成装箱单
     * 2、点击生成装箱单后，后台需要做校验：
     * 多个报运单的装运港、目的港、收货人如果不一致，不能合并报运单装箱，进入错误页面给与提示
     * 多个报运单的装运港、目的港、收货人如果一直，可以进入装箱单添加页面
     *
     * @return
     */
    @RequestMapping("/checkData")
    @ResponseBody
    public HashMap<String, Object> checkData(String id) {
        HashMap<String, Object> map = new HashMap<>();
        String[] split = id.split(",");
        Export export1 = exportService.findById(split[0]);
        if (split.length == 1) {
            if (export1.getState()!=2){
                map.put("message", "只能对已报运的报运单进行装箱");
            }else {
                map.put("message", "true");
            }
        } else {
            Export export2 = exportService.findById(split[1]);
            if (export1.getState() != 2 || export2.getState() != 2) {
                map.put("message", "只能对已报运的报运单进行装箱");
            } else if (!export1.getShipmentPort().equals(export2.getShipmentPort()) ||
                    !export1.getDestinationPort().equals(export2.getDestinationPort()) ||
                    !export1.getConsignee().equals(export2.getConsignee())
            ) {
                map.put("message", "多个报运单的装运港、目的港、收货人不一致，不能合并报运单装箱");
            } else {
                map.put("message", "true");
            }
        }
        return map;
    }


    /**
     * 进入装箱单添加页面
     * 请求地址：http://localhost:8080/cargo/packing/toPacking.do
     * 请求参数：
     * id 购销合同id
     * id 购销合同id
     * 注意：
     * 在web开发中，如果请求参数名称一样的多个值，在后台可以用数组接收，
     * 或者字符串接收，字符串接收的话自动逗号隔开
     */
    @RequestMapping("/toPacking")
    public String toPacking(String id) {
        Packing packing = packingService.findPackingByExport(id);
        // 存储购销合同的id
        request.setAttribute("packing", packing);
        return "cargo/packing/packing-toPacking";
    }

    /**
     * 添加、修改
     */
    @RequestMapping("/edit")
    public String edit(Packing packing) {
        packing.setCompanyId(getLoginCompanyId());
        packing.setCompanyName(getLoginCompanyName());
        // 判断
        if (StringUtils.isEmpty(packing.getPackingListId())) {
            log.info("添加....");
            // 设置创建时间
            packing.setCreateTime(new Date());
            packingService.save(packing);
        } else {
            log.info("修改....");
            packingService.update(packing);
        }
        // 添加成功，重定向到报运单列表
        return "redirect:/cargo/packing/list";
    }

    /**
     * 装箱单列表，点击：查看
     * http://localhost:8080/cargo/contract/toView.do?id=6
     * http://localhost:8080/cargo/contract/submit.do?id=6
     * http://localhost:8080/cargo/contract/cancel.do?id=6
     */
    @RequestMapping("/toView")
    public String toView(String id) {
        log.info("执行购销合同查看.....");
        Packing packing = packingService.findById(id);
        request.setAttribute("packing", packing);
        return "cargo/packing/packing-view";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public String delete(String id) {
        log.info("执行购销合同删除.....");
        packingService.delete(id);
        return "redirect:/cargo/packing/list";
    }


}











